//
//  RCWelcomeViewController.swift
//  RCWeibo
//
//  Created by CL on 15/12/22.
//  Copyright © 2015年 CL. All rights reserved.
//

import UIKit
import SDWebImage

class RCWelcomeViewController: UIViewController {

    override func loadView() {
        view = backImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()


        setupUI()
    }
    //一般在这里进行动画
    override func viewDidAppear(animated: Bool) {
     
        super.viewDidAppear(animated)
        
        showAnimation()
        
    }
    //动画效果
    func showAnimation(){
    
        welcomeLabel.alpha = 0
        
        //改约束
        let offset = -(UIScreen.mainScreen().bounds.height - 200)
        
        iconView.snp_updateConstraints { (make) -> Void in
            
            make.bottom.equalTo(view.snp_bottom).offset(offset)
        }
        
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            //动画执行
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
//                //完成后让label现行
//                UIView.animateWithDuration(0.25, animations: { () -> Void in
//                    
//                    self.welcomeLabel.alpha = 1
//                    
//                })
                
                defaultNotification.postNotificationName(WBSwitchRootVCNote, object: "welcome")
                
        }
        UIView.animateWithDuration(0.25, delay: 0.7, options: [], animations: { () -> Void in
            
            self.welcomeLabel.alpha = 1

            
            }) { (_) -> Void in
                
                
        }
        
    }
    
    //UI设置
    func setupUI(){
    
        //添加subview
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        
        //设置约束
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_centerX)
            
            make.top.equalTo(iconView.snp_bottom).offset(15)
        }
        
        //用sd加载头像
        iconView.sd_setImageWithURL(RCUserAccountViewModel().userHeadIconUrl, placeholderImage: UIImage(named: "avatar_default_big"))
        
        iconView.layer.cornerRadius = 45
//            iconView.bounds.height * 0.5
        iconView.layer.masksToBounds = true
    }

    
    //懒加载
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView = UIImageView()
    
//    private lazy var welcomeLabel: UILabel = {
//    
//        let lb = UILabel()
//        RCUserAccountViewModel().userName
//        lb.text = (RCUserAccountViewModel().userName ?? "") + " 欢迎回来"
////        "\(RCUserAccountViewModel().userName)欢迎回来"
//        lb.font = UIFont.systemFontOfSize(16)
//        lb.textColor = UIColor.lightGrayColor()
//    
//        lb.sizeToFit()
//        return lb
//    }()
    
    private lazy var welcomeLabel: UILabel = UILabel(title: (RCUserAccountViewModel().userName ?? "") + " 欢迎回来", color: UIColor.lightGrayColor(), fontSize: 16)

    

}
